package com.cdream.basic.algorithm.leetcode.map;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cdream
 * @date 10/3/21
 */
public class Question166 {
    public static void main(String[] args) {
        new Question166().fractionToDecimal(-1,-2147483648);
    }
    public String fractionToDecimal(int numerator, int denominator) {
        long numeratorLong = numerator;
        long denominatorLong = denominator;
        if (numeratorLong % denominatorLong == 0) {
            return String.valueOf(numeratorLong / denominatorLong);
        }
        StringBuilder sb = new StringBuilder();

        if (numeratorLong < 0 ^ denominatorLong < 0) {
            sb.append("-");
        }
        numeratorLong = Math.abs(numeratorLong);
        denominatorLong = Math.abs(denominatorLong);

        sb.append(numeratorLong/denominatorLong).append(".");
        Map<Long, Integer> map = new HashMap<>();


        long reminder = numeratorLong % denominatorLong;
        int index = 0;

        StringBuilder sb2 = new StringBuilder();
        while (reminder != 0 && !map.containsKey(reminder)) {
            map.put(reminder, index);

            reminder *= 10;
            sb2.append(reminder / denominatorLong);
            reminder = reminder % denominatorLong;

            index++;
        }
        if (reminder == 0) {
            sb.append(sb2.toString());
            return sb.toString();
        }
        Integer startIndex = map.get(reminder);
        sb2.insert(startIndex, "(");
        sb2.append(")");
        sb.append(sb2.toString());
        return sb.toString();


    }
    public int sum(String prefix) {
        int res = 0;
        for(Map.Entry<String,Integer> entry: new HashMap<String,Integer>().entrySet()){
            if (entry.getKey().startsWith(prefix)){
                res+= entry.getValue();
            }
        }
        return res;
    }

    public int findLHS(int[] nums) {
        Map<Integer, Long> map = Arrays.stream(nums).boxed().collect(Collectors.groupingBy(a -> a,
                Collectors.counting()));
        long res = 0;
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            Integer k = entry.getKey();
            if (map.get(k + 1) != null) {
                res = Math.max(map.get(k) + map.get(k + 1), res);
            }
        }
        return (int) res;

    }
}
